package rpt.yto.flex.remote.data.dao.processor;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import rpt.yto.flex.remote.data.bean.CorpBean;
import rpt.yto.flex.remote.data.biz.QueryBiz;
import rpt.yto.flex.remote.data.dao.itf.IPubObject;
import rpt.yto.util.ConvertFunc;

@SuppressWarnings({"unchecked"})
public class Query_allpsn_province {
	
	private Map<String , String> distinctionMap = null;
	private Map<String , Object> mngDistrictMap = null;
	private Map<String , String> mngDistrictMap2 = null;
	
 	private static Query_allpsn_province querySqlFunc = new Query_allpsn_province();
	
	private Query_allpsn_province() {
	}
	
	public static final Query_allpsn_province getInstance() {
		return querySqlFunc;
	}
	
	/**
	 *   全网按省（区）聚合表 - 全网人员及除总部以外的公司的统计。
	 *   
	 * @param period
	 * @return 
	 * @throws Exception
	 */
	public final StringBuilder getQueryTypeByCorp(String period , Integer rowno , String statistictype , String pk_corp , String type) throws Exception {
		
		StringBuilder builderTypeAll = new StringBuilder();
		
		builderTypeAll.append(" select "+rowno+" rowno , '"+statistictype+"' statistictype , sum(endcount) endcount , sum(endcount_w) endcount_w ,  ");
		builderTypeAll.append(" sum(comcount) comcount , sum(mngcount) mngcount , sum(procount) procount , sum(procount_acc) procount_acc ,  ");
		builderTypeAll.append(" sum(skillcount) skillcount , sum(skillcount_acc) skillcount_acc , sum(expcount) expcount , sum(expcount_acc) expcount_acc ,  ");
		builderTypeAll.append(" sum(roundcount) roundcount , sum(roundcount_acc) roundcount_acc , sum(cscount) cscount from ( ");
		// 期末从业人员数
		builderTypeAll.append(" select count(1) endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
		builderTypeAll.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount "); 
		builderTypeAll.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
		builderTypeAll.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
		builderTypeAll.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
		builderTypeAll.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
		builderTypeAll.append("  and ((bd_psndoc.psnclscope = 0 ");
		builderTypeAll.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
		builderTypeAll.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
		if(type != null && type.equals(IPubObject.TYPE[0])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		else if(type != null && type.equals(IPubObject.TYPE[1])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		
		builderTypeAll.append(" union all  ");
		// 期末女性从业人员数
		builderTypeAll.append(" select 0 endcount , count(1) endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
		builderTypeAll.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
		builderTypeAll.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
		builderTypeAll.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
		builderTypeAll.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
		builderTypeAll.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
		builderTypeAll.append("  and ((bd_psndoc.psnclscope = 0 ");
		builderTypeAll.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
		builderTypeAll.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
		builderTypeAll.append(" and bd_psnbasdoc.sex = '女' ");
		if(type != null && type.equals(IPubObject.TYPE[0])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		else if(type != null && type.equals(IPubObject.TYPE[1])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		builderTypeAll.append(" union all  ");
		// 主管以上管理人员数
		builderTypeAll.append(" select 0 endcount , 0 endcount_w , count(1) comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
		builderTypeAll.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
		builderTypeAll.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
		builderTypeAll.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
		builderTypeAll.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
		builderTypeAll.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
		builderTypeAll.append("  and ((bd_psndoc.psnclscope = 0 ");
		builderTypeAll.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
		builderTypeAll.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
		builderTypeAll.append(" and (om_job.jobname like '%主管' or om_job.jobname like '%经理' or om_job.jobname like '%总监' or om_job.jobname like '%总裁' or om_job.jobname like '%高级专员'  or  om_job.jobname like '%专家') ");
		if(type != null && type.equals(IPubObject.TYPE[0])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		else if(type != null && type.equals(IPubObject.TYPE[1])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		builderTypeAll.append(" union all  ");
		// 经理级以上人员数
		builderTypeAll.append(" select 0 endcount , 0 endcount_w , 0 comcount , count(1) mngcount , 0 procount , 0 procount_acc ,  ");
		builderTypeAll.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
		builderTypeAll.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
		builderTypeAll.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
		builderTypeAll.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
		builderTypeAll.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
		builderTypeAll.append("  and ((bd_psndoc.psnclscope = 0 ");
		builderTypeAll.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
		builderTypeAll.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
		builderTypeAll.append(" and (om_job.jobname like '%经理' or om_job.jobname like '%总监' or om_job.jobname like '%总裁'   or  om_job.jobname like '%专家') ");
		if(type != null && type.equals(IPubObject.TYPE[0])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		else if(type != null && type.equals(IPubObject.TYPE[1])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		builderTypeAll.append(" union all  ");
		// 专业技术人员数
		// procount
		// 取得技术职称人员数
		// procount_acc
		// 技能人员数 
		// skillcount 
		// 取得技能资格证人员数
		// skillcount_acc
		// 快递业务人员数
		builderTypeAll.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
		builderTypeAll.append(" 0 skillcount , 0 skillcount_acc , count(1) expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
		builderTypeAll.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
		builderTypeAll.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
		builderTypeAll.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
		builderTypeAll.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
		builderTypeAll.append("  and ((bd_psndoc.psnclscope = 0 ");
		builderTypeAll.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
		builderTypeAll.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
		builderTypeAll.append(" and (om_job.jobname like '%业务员') ");
		if(type != null && type.equals(IPubObject.TYPE[0])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		else if(type != null && type.equals(IPubObject.TYPE[1])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		builderTypeAll.append(" union all  ");
		// 取得业务员资格证人员数
		builderTypeAll.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
		builderTypeAll.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , count(1) expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
		builderTypeAll.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
		builderTypeAll.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
		builderTypeAll.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
		builderTypeAll.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
		builderTypeAll.append("  and ((bd_psndoc.psnclscope = 0 ");
		builderTypeAll.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
		builderTypeAll.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
		builderTypeAll.append(" and (om_job.jobname like '%业务员') and nvl(bd_psndoc.groupdef1 , 'N') = 'Y' ");
		if(type != null && type.equals(IPubObject.TYPE[0])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		else if(type != null && type.equals(IPubObject.TYPE[1])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		builderTypeAll.append(" union all  ");
		// 分拣操作人员数
		builderTypeAll.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
		builderTypeAll.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , count(1) roundcount , 0 roundcount_acc , 0 cscount ");
		builderTypeAll.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
		builderTypeAll.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
		builderTypeAll.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
		builderTypeAll.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
		builderTypeAll.append("  and ((bd_psndoc.psnclscope = 0 ");
		builderTypeAll.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
		builderTypeAll.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
		builderTypeAll.append(" and (om_job.jobname like '操作%') ");
		if(type != null && type.equals(IPubObject.TYPE[0])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		else if(type != null && type.equals(IPubObject.TYPE[1])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		builderTypeAll.append(" union all  ");
		// 取得操作员资格证人员
		builderTypeAll.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
		builderTypeAll.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , count(1) roundcount_acc , 0 cscount ");
		builderTypeAll.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
		builderTypeAll.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
		builderTypeAll.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
		builderTypeAll.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
		builderTypeAll.append("  and ((bd_psndoc.psnclscope = 0 ");
		builderTypeAll.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
		builderTypeAll.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
		builderTypeAll.append(" and (om_job.jobname like '操作%') and nvl(bd_psndoc.groupdef1 , 'N') = 'Y' ");
		if(type != null && type.equals(IPubObject.TYPE[0])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		else if(type != null && type.equals(IPubObject.TYPE[1])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		builderTypeAll.append(" union all  ");
		// 客服人员数
		builderTypeAll.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
		builderTypeAll.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , count(1) cscount ");
		builderTypeAll.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
		builderTypeAll.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
		builderTypeAll.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
		builderTypeAll.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
		builderTypeAll.append("  and ((bd_psndoc.psnclscope = 0 ");
		builderTypeAll.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
		builderTypeAll.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
		if(type != null && type.equals(IPubObject.TYPE[0])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		else if(type != null && type.equals(IPubObject.TYPE[1])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		builderTypeAll.append(" and (om_job.jobname like '%客%') ) ");
		
		return builderTypeAll;
		
	}
	
	/**
	 * 
	 * 全网按省（区）聚合表 - distinctionMap中的类别
	 * 
	 * @param period
	 * @return
	 * @throws Exception
	 */
	public final StringBuilder getQueryTypeByMap(String period) throws Exception {
		
		if(distinctionMap == null)
			initDistinctionMap();
		
		StringBuilder builderTypeAppend2 = new StringBuilder();
		
		Iterator iter = distinctionMap.entrySet().iterator();
		int rowno = 2;
		while(iter.hasNext()) {
			
			Entry entry = (Entry) iter.next();
			String distinction = (String) entry.getKey();
			String statistictype = (String) entry.getValue();
			
			StringBuilder builderTypeByMap = new StringBuilder();
			
			builderTypeByMap.append(" select "+rowno+" rowno , '"+statistictype+"' statistictype , sum(endcount) endcount , sum(endcount_w) endcount_w ,  ");
			builderTypeByMap.append(" sum(comcount) comcount , sum(mngcount) mngcount , sum(procount) procount , sum(procount_acc) procount_acc ,  ");
			builderTypeByMap.append(" sum(skillcount) skillcount , sum(skillcount_acc) skillcount_acc , sum(expcount) expcount , sum(expcount_acc) expcount_acc ,  ");
			builderTypeByMap.append(" sum(roundcount) roundcount , sum(roundcount_acc) roundcount_acc , sum(cscount) cscount from ( ");
			// 期末从业人员数
			builderTypeByMap.append(" select count(1) endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount "); 
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = '"+distinction+"' ");
			
			builderTypeByMap.append(" union all  ");
			// 期末女性从业人员数
			builderTypeByMap.append(" select 0 endcount , count(1) endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and bd_psnbasdoc.sex = '女' ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = '"+distinction+"' ");
			builderTypeByMap.append(" union all  ");
			// 主管以上管理人员数
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , count(1) comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and (om_job.jobname like '%主管' or om_job.jobname like '%经理' or om_job.jobname like '%总监' or om_job.jobname like '%总裁' or om_job.jobname like '%高级专员'  or  om_job.jobname like '%专家') ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = '"+distinction+"' ");
			builderTypeByMap.append(" union all  ");
			// 经理级以上人员数
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , 0 comcount , count(1) mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and (om_job.jobname like '%经理' or om_job.jobname like '%总监' or om_job.jobname like '%总裁'   or  om_job.jobname like '%专家') ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = '"+distinction+"' ");
			builderTypeByMap.append(" union all  ");
			// 专业技术人员数
			// procount
			// 取得技术职称人员数
			// procount_acc
			// 技能人员数 
			// skillcount 
			// 取得技能资格证人员数
			// skillcount_acc
			// 快递业务人员数
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , count(1) expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and (om_job.jobname like '%业务员') ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = '"+distinction+"' ");
			builderTypeByMap.append(" union all  ");
			// 取得业务员资格证人员数
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , count(1) expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and (om_job.jobname like '%业务员') and nvl(bd_psndoc.groupdef1 , 'N') = 'Y' ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = '"+distinction+"' ");
			builderTypeByMap.append(" union all  ");
			// 分拣操作人员数
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , count(1) roundcount , 0 roundcount_acc , 0 cscount ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and (om_job.jobname like '操作%') ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = '"+distinction+"' ");
			builderTypeByMap.append(" union all  ");
			// 取得操作员资格证人员
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , count(1) roundcount_acc , 0 cscount ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and (om_job.jobname like '操作%') and nvl(bd_psndoc.groupdef1 , 'N') = 'Y' ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = '"+distinction+"' ");
			builderTypeByMap.append(" union all  ");
			// 客服人员数
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , count(1) cscount ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = '"+distinction+"' ");
			builderTypeByMap.append(" and (om_job.jobname like '%客%') ) ");
			
			builderTypeAppend2.append(builderTypeByMap).append(" union all ");
			
			rowno ++;
			
		}
		
		builderTypeAppend2 = new StringBuilder(builderTypeAppend2.substring(0 , builderTypeAppend2.length() - " union all ".length()));
		
		return builderTypeAppend2;
	}
	
	
	public final Map<String , String> initDistinctionMap() throws Exception {
		distinctionMap = new ConcurrentHashMap<String , String>();
		distinctionMap.put("ORGCORP00000000A0015", "总公司人数");
		
		return distinctionMap;
	}
	
	private final void initCorpStringList() throws Exception {
		
		List<Map<String , Object>> fathercorpList = (List<Map<String , Object>>) QueryBiz.getInstance().executeQuery("select pk_corp , unitname  from bd_corp where (unitname like '%管理区%' or unitname = '海南区域公司' or unitname = '广西区域公司' or unitname = '福建区域公司' ) and unitname <> '山东管理区市场部' and bd_corp.isseal = 'N' order by unitcode asc ", CorpBean.class);
		
		mngDistrictMap = new ConcurrentHashMap<String , Object>();
		mngDistrictMap2 = new ConcurrentHashMap<String , String>();
		
		Integer rowno = distinctionMap.size() + 2;
		for(Map<String , Object> fathercorpVO : fathercorpList) {
			
			mngDistrictMap.put("'" + fathercorpVO.get("pk_corp") + "'" , rowno + "~" + fathercorpVO.get("unitname"));
			rowno ++;
			
			String queryCorpSql = "select to_char(wmsys.wm_concat(chr(39) || bd_corp.pk_corp || chr(39))) pk_corp , bd_defdoc.docname unitname from bd_corp left join bd_defdoc on bd_defdoc.pk_defdoc = bd_corp.region where fathercorp = '"+fathercorpVO.get("pk_corp")+"' and bd_corp.isseal = 'N' group by bd_defdoc.docname ";
			List<Map<String , Object>> corpList = (List<Map<String , Object>>) QueryBiz.getInstance().executeQuery(queryCorpSql, CorpBean.class);
			for(Map<String , Object> corpVO : corpList) {
				mngDistrictMap2.put( /* "'" + fathercorpVO.get("pk_corp") + "'," + */ (String) corpVO.get("pk_corp") , rowno + "~" + corpVO.get("unitname"));
				rowno ++;
			}
			
			mngDistrictMap.put("typeChange", mngDistrictMap2);
			
		}
		
	}
	
	public final Map<String , Object> getMngDistrictMap() throws Exception {
		
		if(mngDistrictMap == null)
			initCorpStringList();
		
		return mngDistrictMap;
	}
	
	
	private final String getCorpJoinString(String pk_corp , String type) throws Exception {
		return "1".equals(type) ? " and bd_corp.pk_corp in ( "+ pk_corp +" ) and bd_corp.isseal = 'N' " : " and (bd_corp.pk_corp in ( "+ pk_corp +" ) or bd_corp.fathercorp in ( "+ pk_corp +" )) and bd_corp.isseal = 'N'  ";
		
	}
	
	public final String getQuerySQL(String period) throws Exception {
		
		StringBuilder builderResult = Query_allpsn_province.getInstance().getQueryTypeByCorp(period, 1, "全网合计", null, null)
				.append(" union all ").append(Query_allpsn_province.getInstance().getQueryTypeByMap(period));
		
		Iterator iter = Query_allpsn_province.getInstance().getMngDistrictMap().entrySet().iterator();
		
		while(iter.hasNext()) {
			
			
			Entry entry = (Entry) iter.next();
			String pk_corp = ConvertFunc.getInstance().change(entry.getKey());
			
			if(!"typeChange".equals(pk_corp)) {
				String[] vals = ConvertFunc.getInstance().change(entry.getValue()).split("~");
				
				Integer rowno = Integer.valueOf(vals[0]);
				String statistictype = vals[1];
				
				builderResult.append("union all ").append(Query_allpsn_province.getInstance().getQueryTypeByCorp(period, rowno, statistictype, pk_corp, IPubObject.TYPE[0]));
			} else {
				
				Iterator iterMap2 = ((Map<String, String>) entry.getValue()).entrySet().iterator();
				while(iterMap2.hasNext()) {
					
					Entry entry2 = (Entry) iterMap2.next();
					String pk_corp2 = ConvertFunc.getInstance().change(entry2.getKey());
					String[] vals = ConvertFunc.getInstance().change(entry2.getValue()).split("~");
					Integer rowno = Integer.valueOf(vals[0]);
					String statistictype = vals[1];
					
					builderResult.append("union all ").append(Query_allpsn_province.getInstance().getQueryTypeByCorp(period, rowno, statistictype, pk_corp2, IPubObject.TYPE[1]));
				}
			}
		}
		
		return builderResult.toString();
		
	}

}
